package xanatorium.Zeiterfassung;

import java.util.Calendar;

import xanatorium.Zeiterfassung.data.Entry;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;

public class EditEntry extends Activity
{
	public enum Editstates
	{
		start, stop, full;
	}
	
	private final Editstates state;
	private final Entry entry;
	Button startTime;
	private Button endButton;
	private Button startButton;
	private Button startText;
	private Button endText;
	
	public EditEntry(Editstates start, Entry toEdit)
	{
		super();
		this.state = start;
		this.entry = toEdit;
		
	}
	
	/**
	 * Initialization of the Activity after it is first created. Must at least call
	 * {@link android.app.Activity#setContentView(int)} to describe what is to be displayed in the screen.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editentry);
		String title = null;
		startText = (Button) findViewById(R.id.startText);
		startText.setText("Start Time");
		startButton = (Button) findViewById(R.id.ButtonStart);
		startButton.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
				timePicker.setIs24HourView(true);
				timePicker.setCurrentHour(entry.getStartTime().get(Calendar.HOUR_OF_DAY));
				timePicker.setCurrentMinute(entry.getStartTime().get(Calendar.MINUTE));
				
				timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
				{
					
					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
					{
						entry.setStartHour(hourOfDay);
						entry.setStartMinute(minute);
					}
				});
				setContentView(timePicker.getId());
			}
		});
		endText = (Button) findViewById(R.id.endText);
		endText.setText("End Time");
		endButton = (Button) findViewById(R.id.ButtonEnd);
		endButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
				timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
				timePicker.setCurrentMinute(Calendar.getInstance().get(Calendar.MINUTE));
				timePicker.setIs24HourView(true);
				timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
				{
					
					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
					{
						entry.setEndHour(hourOfDay);
						entry.setEndMinute(minute);
					}
				});
				setContentView(timePicker.getId());
			}
		});
		
		switch (state)
		{
			case start:
				title = "start Entry";
				startText.setVisibility(View.VISIBLE);
				startButton.setVisibility(View.VISIBLE);
				endText.setVisibility(View.GONE);
				endButton.setVisibility(View.GONE);
				break;
			case stop:
				title = "finalize Entry";
				startText.setVisibility(View.GONE);
				startButton.setVisibility(View.GONE);
				endText.setVisibility(View.VISIBLE);
				endButton.setVisibility(View.VISIBLE);
				break;
			case full:
			default:
				title = "edit Entry";
				startText.setVisibility(View.VISIBLE);
				endText.setVisibility(View.VISIBLE);
				startButton.setVisibility(View.VISIBLE);
				endButton.setVisibility(View.VISIBLE);
				break;
		}
		
		setTitle(title);
	}
}